﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Calendar;
using C4F.DevKit.Calendar.Components;

namespace C4F.DevKit.Calendar.Samples
{
    public partial class AppointmentDialog : Form
    {
        // whether or not the appointment was already in the calendar (which means we're editing it)
        private bool _apptExists;

        public AppointmentDialog(CalendarService calService)
        {
            InitializeComponent();

            calendarService1 = calService;
            _apptExists = false;
        }

        public AppointmentDialog(CalendarService calService, Appointment appt)
            : this(calService)
        {
            appointmentDetails1.Appointment = appt;
            _apptExists = true;
        }

        private void appointmentDetails1_Load(object sender, EventArgs e)
        {

        }

        public Appointment Appointment
        {
            get
            {
                return appointmentDetails1.Appointment;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (_apptExists)
                {
                    calendarService1.EditAppointment(appointmentDetails1.Appointment);
                }
                else
                {
                    calendarService1.AddAppointment(appointmentDetails1.Appointment);
                }

                ((MainForm)MdiParent).RefreshAppointmentList();
                Close();
            }
            catch (CalendarException)
            {
                MessageBox.Show("Appointment must have a subject, and the end of the appointment must be later or the same time as the start", "Invalid appointment");
            }
            catch (Exception)
            {
                MessageBox.Show(
                    String.Format("Error saving new appointment to calendar {0}. You may not have write permission on this file.", calendarService1.CalendarFilePath),
                    "Error while adding new appointment");
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.appointmentDetails1.AttendeesList.Add(new Attendee { Name = "ThisMe" });
        }            
    }
}
